package adapter;

import android.app.ActionBar;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Bean.Problems;

public class LearnMoreRightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnLongClickListener,View.OnClickListener{
    private Context mContext;
    private List<Problems> learnQueslist;
    private RelativeLayout repop;

    public LearnMoreRightAdapter(Context context){
        mContext=context;
        learnQueslist= new ArrayList<>();
    }

    public void setnewData(List<Problems> learnQueslists){
        if(learnQueslist != null){
            learnQueslist.clear();
//            LogUtils.d( learnQueslist.size()+"--------最后长度啊-----------");
            learnQueslist.addAll(learnQueslists);
        }
//        LogUtils.d( learnQueslist.size()+"--------参数长度-----------");
    }


    public void setupData(List<Problems> learnQueslists,RelativeLayout repops){
        if(learnQueslist !=null){
            learnQueslist.clear();
            repop=repops;
//            LogUtils.d( learnQueslist.size()+"--------第一次开始长度-----------");
            learnQueslist.addAll(learnQueslists);
//            LogUtils.d( learnQueslist.size()+"--------第一次结束长度-----------");
        }
        this.notifyDataSetChanged();
    }


    private OnItemClickListener mOnItemClickListener = null;
    private OnLongClickListener mOnItemLongClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static interface OnLongClickListener {
        boolean onLongItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnLongItemClickListener(OnLongClickListener listener1) {
        this.mOnItemLongClickListener = listener1;
    }


    @Override
    public int getItemCount() {
//        LogUtils.d("learnQueslist.size()"+learnQueslist.size());
        return learnQueslist.size();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        return mOnItemLongClickListener != null && mOnItemLongClickListener.onLongItemClick(v, (int) v.getTag());
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        HeadHolder headHolder = (HeadHolder) holder;
//        LogUtils.d( learnQueslist.size()+"--------最后长度我日-----------");
        if (learnQueslist.size()>0){
            headHolder.learn_righttopcontent.setText(learnQueslist.get(position).getTitle());
        }
        if (learnQueslist.size()==position+1 && learnQueslist.size() >1){
            headHolder.laren_righttopviw.setVisibility(View.INVISIBLE);
        }else {
            headHolder.laren_righttopviw.setVisibility(View.VISIBLE);
        }
        if (position == 0){
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( headHolder.viewright.getLayoutParams());
            lp.setMargins(0, 20 * learnQueslist.size(), 0, 0);
            headHolder.viewright.setLayoutParams(lp);
//        }else if(position == 0 && learnQueslist.size() <2 ){
//            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( headHolder.viewright.getLayoutParams());
//            lp.setMargins(0, 40 , 0, 0 );
//            headHolder.viewright.setLayoutParams(lp);
//        }else if(position == learnQueslist.size()-1  && learnQueslist.size() <2 ){
//            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( headHolder.viewright.getLayoutParams());
//            lp.setMargins(0, 0 , 0, 40 );
//            headHolder.viewright.setLayoutParams(lp);
        }else if(position == learnQueslist.size() -1){
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( headHolder.viewright.getLayoutParams());
            lp.setMargins(0, 0, 0, 20 * learnQueslist.size());
            headHolder.viewright.setLayoutParams(lp);
        }else {
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( headHolder.viewright.getLayoutParams());
            lp.setMargins(0, 0, 0, 0);
            headHolder.viewright.setLayoutParams(lp);
        }

        headHolder.learn_rightitem.setOnClickListener(v->{
            if (!learnQueslist.get(position).getTitle().equals("")) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                View view1 = inflater.inflate(R.layout.popup, null);//从父布局传了一个布局过来，显示在全屏
                PopupWindow popupWindow = new PopupWindow(view1, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);//这个设置全为宽
                popupWindow.setOutsideTouchable(true);  // 设置PopupWindow是否能响应外部点击事件
                popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
                popupWindow.update();
                popupWindow.setTouchable(true); // 设置PopupWindow是否能响应点击事件
                popupWindow.setFocusable(true);    //获取焦点
                popupWindow.showAsDropDown(repop); //显示在某个布局下面
                ImageView xx = view1.findViewById(R.id.popimg);
                TextView textView1 = view1.findViewById(R.id.text);
                TextView textView2 = view1.findViewById(R.id.textcontent);
                textView1.setText(learnQueslist.get(position).getTitle());
                textView2.setText(learnQueslist.get(position).getContent());
                xx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
            }
        });
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_learnbottomrightreycleview, parent, false);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
//        view.setOnLongClickListener(this);
        return new HeadHolder(view);
    }

    /**
     * 头布局Holder
     */
    static class HeadHolder extends RecyclerView.ViewHolder {
        private TextView learn_righttopcontent;
        private RelativeLayout learn_rightitem;
        private View laren_righttopviw;
        private View viewright;
        public HeadHolder(View itemView) {
            super(itemView);
            learn_righttopcontent = itemView.findViewById(R.id.learn_righttopcontent);
            learn_rightitem= itemView.findViewById(R.id.learn_rightitem);
            laren_righttopviw= itemView.findViewById(R.id.laren_righttopviw);
            viewright = (View) itemView.findViewById(R.id.viewright);
        }
    }
}
